import os
import sys
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg19 import preprocess_input
from keras.applications.vgg19 import decode_predictions
from keras.applications.vgg19 import VGG19
from keras.models import Model
import numpy as np
import json
import cv2
import time
from pymongo import MongoClient
import pymongo
img_path = 'E:\\image_pi\\'
videolist={}
client = MongoClient(port=27017)
db=client['tiktok']
i=0
namepi='merrybossmas'
collist=['merrybossmas']
for col in collist:
    videolist[col]=[]
    for item in db[col].find():
        videolist[col].append(item['_id'])
for key in videolist.keys():
    print(key,len(videolist[key]))
c=0
os.system('mp4box -hint')
for hashtag in videolist.keys():
    for vid in videolist[hashtag]:
        c+=1
        if not os.path.exists('E:\\Tiktok\\Hashtag\\' + hashtag + '\\' + vid + '.mp4'):
            continue
        vname=vid

        vidcap = cv2.VideoCapture('E:\\Tiktok\\Hashtag\\' + hashtag + '\\' + vid + '.mp4')
        count = 0
        vidcap.set(cv2.CAP_PROP_POS_MSEC, (count * 5000))
        success, image = vidcap.read()

        while success:
            vidcap.set(cv2.CAP_PROP_POS_MSEC, (count * 5000))
            if os.path.exists(img_path + vname + "_%d.jpg" % count+'.jpg'):
                count+=1
                continue
            cv2.imwrite(img_path + vname + "_%d.jpg" % count, image[round(len(image)/10):round(len(image)-len(image)/10)])
            success, image = vidcap.read()
            count = count + 1
            if count>12:
                break
idlist=[]
for ht in videolist.keys():
    idlist.extend(videolist[ht])
with open('E:\\data_pi\\imglist_temp'+namepi+'.txt','w') as file_input:
    c=0
    for vname in os.listdir(img_path):
        v=vname.split('_')[0]
        if vname.endswith('.jpg') and v in idlist:
            c+=1
            file_input.write(img_path+vname+'\n')
    print(c)
os.system('"'+'py -2 D:\\Work\\kusuri\\DeepSentiBank\\sentiBank.py '+'E:\\data_pi\\imglist_temp'+namepi+'.txt'+'"')


base_model = VGG19(weights='imagenet')
model = Model(base_model.input, base_model.get_layer('fc2').output)
col=''
with open ('E:\\data_pi\\imglist_temp'+namepi+'.txt','r',) as txt_input:
    with open ('E:\\data_pi\\imglist_temp'+namepi+'.json','r',) as json_input:
        senti=json.load(json_input)
        c=0
        prev=''
        l=0
        avg=[]
        for line in txt_input:
            line=line.replace(img_path,'')
            print(line,c)
            iid=line.split('_')[0]
            imgnum=line.split('_')[1].replace('.jpg','').strip()
            for ht in videolist.keys():
                if iid in videolist[ht]:
                    col=ht
                    break
            ori = db[col].find_one({'_id': iid})
            if imgnum in ori['video_feature']['img_embed'].keys():
                c+=1
                continue
            print(iid)
            tempresult=[]
            line=line.strip()
            img = load_img(img_path+line,target_size=(224, 224))
            img = img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img = preprocess_input(img)
            fc2_features = model.predict(img)
            prob_vgg = decode_predictions(base_model.predict(img), top=1000)
            for item in sorted(prob_vgg[0]):
                tempresult.append(float(item[2]))
            for item in sorted(senti['images'][c]['bi-concepts']):
                tempresult.append(float(senti['images'][c]['bi-concepts'][item]))
            for item in senti['images'][c]['features']:
                tempresult.append(float(item))
            for ht in videolist.keys():
                if iid in videolist[ht]:
                    col=ht
                    break
            db[col].update_one({'_id': iid}, {'$set':{'video_feature.img_embed.'+imgnum:tempresult}})
            print(db[col].find_one({'_id': iid}))
            c+=1
